package cn.tedu.store.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.store.bean.ResponseResult;
import cn.tedu.store.bean.User;
import cn.tedu.store.service.IUserService;
import cn.tedu.store.service.ex.PassordNotMatchException;
import cn.tedu.store.service.ex.UsernameAlreadyExistsException;
import cn.tedu.store.service.ex.UsernameNotFoundException;

@RequestMapping("/user")
@Controller
public class UserController extends BaseController {
	
	@Resource(name="userService")
	private IUserService userService;
	
	@RequestMapping("/register.do")
	public String showRegister() {
		return "register";		
	}
	@RequestMapping("/profile.do")
	public String showProfile(ModelMap modelMap,HttpSession session) {
		Integer id=getUidFromSession(session);
		User user=userService.findUserById(id);
		modelMap.addAttribute("user", user);
		return "user_profile";		
	}
	@RequestMapping("/check_username.do")
	@ResponseBody
	public ResponseResult<Void> checkUsername(String username){
		ResponseResult<Void> rr;
		
		boolean result=userService.checkUsernameExists(username);
		if(result) {
			//表示用户名已经存在
			rr=new ResponseResult<Void>(0, "用户名已经被占用");
		}else {
			rr=new ResponseResult<Void>(1, "用户名可以使用");
		}
		return rr;
	}
	
	@RequestMapping("/check_email.do")
	@ResponseBody
	public ResponseResult<Void> checkEmail(String email){
		ResponseResult<Void> rr;
		
		boolean result=userService.checkEmailExists(email);
		if(result) {
			//表示邮箱已经存在
			rr=new ResponseResult<Void>(0, "邮箱已经被占用");
		}else {
			rr=new ResponseResult<Void>(1, "邮箱可以使用");
		}
		return rr;
	}
	
	@RequestMapping("/check_phone.do")
	@ResponseBody
	public ResponseResult<Void> checkPhone(String phone){
		ResponseResult<Void> rr;
		
		boolean result=userService.checkPhoneExists(phone);
		if(result) {
			//表示手机已经存在
			rr=new ResponseResult<Void>(0, "手机已经被占用");
		}else {
			rr=new ResponseResult<Void>(1, "手机可以使用");
		}
		return rr;
	}
	@RequestMapping(value="/login.do")
	public String showLogin() {
		return "login";
	}
	@RequestMapping(value="/password.do")
	public String showPassword() {
		return "user_password";
	}
	@RequestMapping(value="/handle_login.do",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> handleLogin(@RequestParam("username") String username,
			@RequestParam("password") String password,@RequestParam("code") String code,HttpSession session){
		ResponseResult<Void> rr;
		try {
			String c=(String)session.getAttribute("code");
			if(c==null) {
				return new ResponseResult<Void>(0, "验证失败");
			}
			//忽略大小写的比较
			if(!c.equalsIgnoreCase(code)) {
				return new ResponseResult<Void>(0, "验证失败");
			}
			User user = userService.login(username, password);
			rr=new ResponseResult<Void>(1);
			session.setAttribute("uid", user.getId());
			session.setAttribute("username", user.getUsername());
		} catch (UsernameNotFoundException e) {
			rr=new ResponseResult<Void>(0, e.getMessage());
		}catch (PassordNotMatchException e) {
			rr=new ResponseResult<Void>(-1, e.getMessage());
		}
		return rr;
	}
	
	@RequestMapping(value="/handle_register.do",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> handleRegister(
			@RequestParam("uname") String username,
			@RequestParam("upwd") String password,
			@RequestParam("phone") String phone,
			@RequestParam("email") String email){
		//声明返回值
		ResponseResult<Void> rr; 
		User user=new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setPhone(phone);
		user.setEmail(email);
		try {
			userService.register(user);
			rr=new ResponseResult<Void>(1, "注册成功");
			return rr;
		} catch (UsernameAlreadyExistsException e) {
			rr=new ResponseResult<Void>(0, e.getMessage());
			return rr;
		}
	}
	
	@RequestMapping("/logout.do")
	public String handleLogout(HttpServletRequest request,HttpSession session) {
		session.invalidate();
		String url="../main/index.do";
		return "redirect:"+url;
	}
	
	@RequestMapping(value="/handlePassword.do",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> handleChangePassword(
			HttpSession session,
			@RequestParam("new_password") String newPassword,
			@RequestParam("old_password") String oldPassword){
		ResponseResult<Void> rr;
		Integer id=getUidFromSession(session);
		try {
			userService.changePassword(id, oldPassword, newPassword);
			rr=new ResponseResult<Void>(1, "修改成功");
		} catch (UsernameNotFoundException e) {
			rr=new ResponseResult<Void>(0, e.getMessage());
		}catch (PassordNotMatchException e) {
			rr=new ResponseResult<Void>(-1, e.getMessage());
		}
		return rr;
}
	@RequestMapping(value="/handle_edit_profile.do",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> handleEditProfile(
			String username,
			Integer gender,
			String phone,
			String email,
			HttpSession session
			){
		ResponseResult<Void> rr;
		Integer id=getUidFromSession(session);
		try {
			userService.editProfile(id, username, gender, phone, email);
			//更新Session
			User u=userService.findUserById(id);
			session.setAttribute("username", u.getUsername());
			rr=new ResponseResult<Void>(1, "修改成功");
		} catch (UsernameNotFoundException e) {
			rr=new ResponseResult<Void>(0, e.getMessage());
		}catch(UsernameAlreadyExistsException e) {
			rr=new ResponseResult<Void>(-1, e.getMessage());
		}
		return rr;
	}
}	
